home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Chat & Communication / Digsby build 37 / digsby_setup.exe / lib / yahoo / yfiletransfer.pyo (.txt) < prev   
Python Compiled Bytecode  |  2008-10-13  |  7KB  |  208 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. from __future__ import with_statement
  5. from httplib import IncompleteRead
  6. FILE_XFER_URL = 'http://filetransfer.msg.yahoo.com:80/notifyft'
  7. PROXY_FILESIZE_LIMIT = 1048576
  8. import common
  9. from traceback import print_exc
  10. from util import threaded, chained_files
  11. from yahoo.yahooutil import filename_from_url
  12. import struct
  13. from StringIO import StringIO
  14. from util.net import HTTPConnProgress
  15. from logging import getLogger
  16. log = getLogger('yahoo.ft')
  17. info = log.info
  18. error = log.error
  19. in_cls = common.IncomingHTTPFileTransfer
  20. out_cls = common.filetransfer.OutgoingFileTransfer
  21. PNG_HEADER = struct.pack('8B', 137, 80, 78, 71, 13, 10, 26, 10)
  22.  
  23. class YahooHTTPIncoming(in_cls):
  24.     
  25.     def __init__(self, protocol, buddy, url):
  26.         in_cls.__init__(self, protocol, buddy, filename_from_url(url), url)
  27.  
  28.  
  29.  
  30. def set_buddy_icon(yahoo, image_data):
  31.     log.info('set_buddy_icon %r', yahoo)
  32.     YahooIconSender.send_buddy_icon(yahoo, image_data)
  33.  
  34.  
  35. class OutgoingYHTTPXfer(out_cls):
  36.     direction = 'outgoing'
  37.     http_path = '/notifyft'
  38.     host = 'filetransfer.msg.yahoo.com:80'
  39.     conn_host = 'filetransfer.msg.yahoo.com'
  40.     
  41.     def __init__(self, protocol, buddy = None, fileinfo = None, initiate = True):
  42.         out_cls.__init__(self)
  43.         if fileinfo is not None:
  44.             self.filepath = fpath = fileinfo.files[0]
  45.             self.name = fpath.name
  46.             self.size = fpath.size
  47.         
  48.         self.buddy = buddy
  49.         self.protocol = protocol
  50.         self.cancelled = False
  51.         if hasattr(fileinfo, 'obj'):
  52.             fileinfo.obj.close()
  53.         
  54.         if initiate:
  55.             self.initiate_transfer()
  56.         
  57.  
  58.     
  59.     def initiate_transfer(self, message = ''):
  60.         filesize = self.filepath.size
  61.         if filesize > PROXY_FILESIZE_LIMIT:
  62.             self.state = self.states.PROXY_XFER_FILESIZE_ERROR
  63.             return None
  64.         
  65.         protocol = self.protocol
  66.         ydata = protocol.yahoo_packet_v(15, 'filetransfer', 'available', 'away_buddy', protocol.self_buddy.name, 'to', self.buddy.name, 'message', message, 'filename', self.name, 'filesize', filesize, 'filedata', '')
  67.         sublen = (struct.unpack('!H', ydata[8:10])[0] - 8 + len(str(filesize))) + 14
  68.         ydata = ydata[:8] + struct.pack('!H', sublen - 1) + ydata[10:-2]
  69.         self.state = self.states.TRANSFERRING
  70.         
  71.         try:
  72.             fileobj = file(self.filepath, 'rb')
  73.         except:
  74.             print_exc()
  75.             self.state = self.states.CONN_FAIL
  76.             self.on_error()
  77.  
  78.         self._post_file(ydata, protocol.cookie_str, fileobj = fileobj, filesize = filesize)
  79.  
  80.     
  81.     def cancel(self):
  82.         
  83.         try:
  84.             self.conn.close()
  85.             del self.conn
  86.         except:
  87.             pass
  88.  
  89.         self.cancelled = True
  90.         self.state = self.states.CANCELLED_BY_YOU
  91.  
  92.     
  93.     def _post_file(self, ydata, cookies, fileobj = None, filesize = None):
  94.         
  95.         try:
  96.             self.conn = conn = HTTPConnProgress(self.conn_host)
  97.         except Exception:
  98.             e = None
  99.             print_exc()
  100.             if not self.cancelled:
  101.                 self.state = self.states.CONN_FAIL
  102.                 self.on_error()
  103.             
  104.             return False
  105.  
  106.         conn.putrequest('POST', self.http_path, skip_host = True, skip_accept_encoding = True)
  107.         conn.putheader('Cookie', cookies)
  108.         conn.putheader('Host', self.host)
  109.         conn.putheader('Content-Length', str(len(ydata) + filesize))
  110.         conn.putheader('Cache-Control', 'no-cache')
  111.         conn.endheaders()
  112.         diff = len(ydata) - filesize
  113.         
  114.         try:
  115.             if ydata != '':
  116.                 conn.send_file_cb(chained_files([
  117.                     ydata,
  118.                     fileobj]), self._setcompleted, progressDelta = len(ydata))
  119.             else:
  120.                 conn.send_file_cb(fileobj, self._setcompleted)
  121.         except:
  122.             if not self.cancelled:
  123.                 print_exc()
  124.                 self.state = self.states.CONN_FAIL
  125.             
  126.             
  127.             try:
  128.                 conn.close()
  129.  
  130.         finally:
  131.             
  132.             try:
  133.                 fileobj.close()
  134.             except:
  135.                 pass
  136.  
  137.  
  138.         if self.state != self.states.TRANSFERRING:
  139.             return None
  140.         
  141.         self._setcompleted(filesize)
  142.         
  143.         try:
  144.             response = conn.getresponse()
  145.             respdata = response.read()
  146.             status = response.status
  147.             log.info('response data %d bytes, status code %s', len(respdata), status)
  148.             log.info('response data %s', respdata)
  149.             if status != 200:
  150.                 self.state = self.states.CONN_FAIL_XFER
  151.                 log.error('ERROR: POST returned a status of %d', status)
  152.                 return False
  153.             
  154.             info('HTTP POST response status %d', status)
  155.         except IncompleteRead:
  156.             if ydata != '':
  157.                 raise 
  158.             
  159.         except:
  160.             ydata != ''
  161.         finally:
  162.             conn.close()
  163.  
  164.         self.state = self.states.FINISHED
  165.         self._ondone()
  166.         return True
  167.  
  168.     _post_file = threaded(_post_file)
  169.  
  170.  
  171. class YahooIconSender(OutgoingYHTTPXfer):
  172.     
  173.     def __repr__(self):
  174.         return '<YahooIconSender>'
  175.  
  176.     
  177.     def send_buddy_icon(cls, protocol, image_data):
  178.         Image = Image
  179.         import PIL
  180.         i = Image.open(StringIO(image_data))
  181.         if image_data[:8] != PNG_HEADER or i.size != (96, 96):
  182.             converted = StringIO()
  183.             i2 = i.Resized(96)
  184.             i2.save(converted, 'PNG')
  185.             image_data = converted.getvalue()
  186.         
  187.         xfer = YahooIconSender(protocol, initiate = False)
  188.         selfname = protocol.self_buddy.name
  189.         filesize = len(image_data)
  190.         ydata = protocol.yahoo_packet_v(15, 'picture_upload', 'available', 'frombuddy', selfname, 'expires', struct.pack('6s', '604800'), 'away_buddy', selfname, 'filesize', filesize, 'filename', 'myicon.png', 'message', ' ', 'filedata', '')
  191.         sublen = (struct.unpack('!H', ydata[8:10])[0] - 8 + len(str(filesize))) + 14
  192.         ydata = ydata[:8] + struct.pack('!H', sublen - 1) + ydata[10:-2]
  193.         xfer.state = xfer.states.TRANSFERRING
  194.         log.info('sending %d bytes of image data', filesize)
  195.         xfer._post_file(ydata, protocol.cookie_str, fileobj = StringIO(image_data), filesize = filesize)
  196.  
  197.     send_buddy_icon = classmethod(send_buddy_icon)
  198.     
  199.     def _ondone(self):
  200.         self.setnotifyif('state', self.states.FINISHED)
  201.         self._done = True
  202.  
  203.     
  204.     def on_error(self):
  205.         log.critical('error sending icon')
  206.  
  207.  
  208.